package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class GaoJiaoSheWenKe {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "acw_tc=968a799b15819141755303294e2b040406d48e1c0a1d3d294e1b367b56; JSESSIONID=7CD409C989E3DB10F1FA5CAD173245DE; Hm_lvt_4d48f696b158673f53e504194d672fa6=1581991676,1581991713,1582200113,1582200122; Hm_lpvt_4d48f696b158673f53e504194d672fa6=1582200122";
    public static final String ORIGIN = "https://ct.hep.com.cn";
    public static final String REFERER = "https://ct.hep.com.cn/public/page/wk.html?from=singlemessage";
    public static final String URL_PRE = "http://book.sciencereading.cn";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum = 0;
        int totalPage = 26;
        List<CxgBook> listBook = new ArrayList<>();
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "https://ct.hep.com.cn/public/page/wk-listData.html";
            Map<String, Object> parameter = MapUtil.newHashMap(4);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("start", i * 45);
            jsonObject.put("limit", "45");
            jsonObject.put("condition", ArrayUtil.newArray(1));
            String encode = URLUtil.encode(jsonObject.toJSONString());
            parameter.put("pageConfig", encode);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Origin", ORIGIN);
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(url, headers, parameter);
            while (result.contains("页面没有找到")) {
                //睡眠2秒继续访问
                Thread.sleep(2000);
                result = HttpUtil.doPost(url, headers, parameter);
            }
            if (StrUtil.isNotBlank(result)) {
                Document htmlDoc = null;
                try {
                    htmlDoc = Jsoup.parseBodyFragment(result);
                } catch (Exception e) {
                    log.warn("jsoup解析html发生错误");
                }
                if (htmlDoc == null) {
                    continue;
                }
                Elements elements = htmlDoc.select(".row");
                if (elements != null && elements.size() > 0) {
                    for (Element ele : elements) {
                        CxgBook cb = new CxgBook();
                        Elements elements1 = ele.select("div");
                        Element element = elements1.select(".col-xs-12").first();
                        Element element1 = elements1.select(".name").first();
                        Element element2 = elements1.select(".btnDown").first();
                        cb.setBookName(element.text());
                        cb.setAuthor(element1.text());
                        cb.setDigitalUri(element2.select("a").first().attr("href"));
                        listBook.add(cb);
                    }
                }
            }
        }
        write2Excel(listBook);
    }

    @Test
    public void writeTest() {
        write2Excel(new ArrayList<>());
    }

    public void write2Excel(List<CxgBook> listBook) {
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/高教文科资源.xlsx");
        //通过构造方法创建writer
        //ExcelWriter writer = new ExcelWriter("d:/writeTest.xls");

        //跳过当前行，既第一行，非必须，在此演示用
        writer.passCurrentRow();
        //一次性写出内容，强制输出标题
        writer.write(listBook, true);
        //关闭writer，释放内存
        writer.close();
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
